<?php

namespace app\data\command;

use app\admin\service\WxWorkService;
use app\data\service\AliSingleCallService;
use think\admin\Command;
use think\admin\Exception;
use think\console\Input;
use think\console\Output;
use think\facade\Log;

/**
 * 未回复消息通知
 * Class NotReplyRecordNotice
 * @package app\data\command
 */
class RecordRemarkRemind extends Command
{
    protected function configure()
    {
        $this->setName('record_remark_remind');
        $this->setDescription('备注提醒');
    }

    /**
     * 业务指令执行
     * @param Input $input
     * @param Output $output
     * @return void
     * @throws Exception
     */
    protected function execute(Input $input, Output $output)
    {
        while (true) {
            $min = date('Y-m-d H:i:s');

            $list = $this->app->db->name('data_record_remark_log')
                ->where('need_remind', 1)
                ->where('is_remind', 0)
                ->where('remind_at', '<', $min)
                ->select()->toArray();

            if (!empty($list)) {
                $uuidDockingUserIds = array_column($list, 'uuid_docking');
                $users = $this->app->db->name('work_user')->whereIn('id', array_unique($uuidDockingUserIds))->field('userid,id')->select()->toArray();
                foreach ($users as $user) {
                    $mapId2WorkUserId[$user['id']] = $user['userid'];
                }

                foreach ($list as $item) {
                    $userid = $mapId2WorkUserId[$item['uuid_docking']] ?? 0;
                    if ($userid && $item['remark']) {
                        try {
                            var_dump($userid, $item);
                            $this->app->db->name('data_record_remark_log')->where('id', $item['id'])->update(['is_remind' => 1]);
//                            WxWorkService::instance()->sendMessageTextByUserid($userid, $item['remark']);

                        } catch (\Exception $exception) {
                            Log::error("备注提醒-报错: " . json_encode([
                                'message' => $exception->getMessage(),
                                'file' => $exception->getFile(),
                                'line' => $exception->getLine(),
                                'trace' => $exception->getTraceAsString(),
                            ]));
                        }
                    }
                }
            }

            sleep(30);
        }
    }




}